<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>51微贷</title>
		<link rel="stylesheet" type="text/css" href="../css/mui.min.css">
		<link rel="stylesheet" type="text/css" href="../css/reset.css">
		<link rel="stylesheet" type="text/css" href="../css/common.css">
		<link rel="stylesheet" type="text/css" href="../css/icomoon/style.css">
		<script type="text/javascript" src="../js/rem.js"></script>
	</head>
	<style>
		.wrapper {
			position: absolute;
			width: 100%;
			top: 1.28rem;
			bottom: 0;
		}
	</style>

	<body class="bg" onload="loaded()">
		<div class="m-bindbank m-bankcard">
			<h3 class="header f-pr">
				<span>我的银行卡</span>
				<i class="icon-angle-left f-pa"></i>
				<small class="f-pa" id="manage">添加</small>
			</h3>
			<div class="wrapper" id="wrapper">
				<div id="scroller">
					<div class="bankcard-box"></div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../js/md5.min.js"></script>
		<script type="text/javascript" src="../js/zepto.min.js"></script>
		<script type="text/javascript" src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../js/iscroll.js"></script>
		<script type="text/javascript" src="../js/scroller.js"></script>

		<script type="text/javascript">
			mui.plusReady(function() {
				function getBankList() {
					mui.ajax(base + 'account/userbankcardlist', {
						type: 'get',
						data: {
							'user': parseInt(plus.storage.getItem('user_id'))
						},
						dataType: 'json',
						headers: sign(md5),
						success: function(data) {
							console.log('bankcard:' + JSON.stringify(data))

							var list = data,
								result = '';
							for(var i = 0, len = list.length; i < len; i++) {
								var item = list[i];
								var bank_logo = banklogo(item.bank_code);
								result += '<div class="bankcard">';
								result += '<div class="name">';
								result += '<span class="logo f-fl"><img src="' + bank_logo + '"></span>';
								result += '<span>' + item.bank_name + '</span></div>';
								result += '<p class="cardno">' + item.bank_no + "</p>";
								result += '<p class="limit clearfix">';
								if(item.is_default) {
									result += '<span class="f-fl disable">默认卡</span>';
									result += '<span class="f-fr">';
									if(item.is_audit) {
										result += '<span class="disable pr">已验证</span></span><p>';
									} else {
										result += '<span class="disable pr">未验证</span></span><p>';
									}
								} else {
									result += '<span class="default f-fl" data-id="' + item.id + '">设置默认卡</span>';
									result += '<span class="f-fr">';
									if(item.is_audit) {
										result += '<span class="disable">已验证</span>';
									} else {
										result += '<span class="disable">未验证</span>';
									}
									result += '<span class="bind" data-id="' + item.id + '">解绑</span></span></p>';
								}
								result += '</div>';
							}
							$('.bankcard-box').html(result);

							$('.bind').on('tap', function() {
								var id = parseInt($(this).attr('data-id'));
								unbindcard(id);
							});

							$('.default').on('tap', function() {
								var id = parseInt($(this).attr('data-id'));
								setdefaultcard(id);
							});
						},
						error: function() {
							console.error('error');
						}
					});
				}

				getBankList();

				var btnArray = ['确认', '取消'];

				function setdefaultcard(id) {
					mui.confirm('确认设置默认卡吗？', '提示', btnArray, function(e) {
						if(e.index == 0) {
							var data = {
								'id': id,
								'user': parseInt(plus.storage.getItem('user_id'))
							};
							mui.ajax(base + 'account/setdefaultcard', {
								type: 'post',
								data: data,
								dataType: 'json',
								headers: sign(md5),
								success: function(data) {
									if(data.result) {
										mui.toast('设置默认卡成功');
										getBankList();
									} else {
										mui.toast(data.message);
									}
								},
								error: function() {
									console.error('error');
								}
							});
						} else {
							mui.toast('您取消了设置该卡为默认卡的操作');
						}
					});
				}

				function unbindcard(id) {
					mui.confirm('确认解绑吗？', '提示', btnArray, function(e) {
						if(e.index == 0) {
							mui.ajax(base + 'account/unbindcard', {
								type: 'post',
								data: {
									'id': id,
									'user': parseInt(plus.storage.getItem('user_id'))
								},
								dataType: 'json',
								headers: sign(md5),
								success: function(data) {
									if(data.result) {
										mui.toast('解绑成功');
										getBankList();
									} else {
										mui.toast(data.message);
									}
								},
								error: function() {
									console.error('error');
								}
							});
						} else {
							mui.toast('您取消了解绑该卡的操作');
						}
					});
				}

				$('#manage').on('tap', function() {
					mui.openWindow({
						url: 'bindbankcard.html'
					});
				});

				$('.icon-angle-left').on('tap', function() {
					mui.back();
				});
			});
		</script>
	</body>

</html>